package com.lesso.lll.sunflower.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.lesso.lll.sunflower.core.ApiResponse;
import com.lesso.lll.sunflower.resource.config.RabbitMQConfig;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import pojo.dto.EmployeeDTO;
import pojo.dto.EmployeeQueryDTO;
import pojo.vo.EmployeeVO;
import service.IEmployeeService;

import javax.validation.Valid;

/**
 * <p>
 * 员工基础信息 前端控制器
 * </p>
 *
 * @author lll
 * @since 2025-07-02
 */
@RestController
@RequestMapping("/base/employee")
public class EmployeeController {
    @DubboReference
    private IEmployeeService employeeService;

    private final StringRedisTemplate stringRedisTemplate;
    private final AmqpTemplate amqpTemplate;

    public EmployeeController(StringRedisTemplate stringRedisTemplate, AmqpTemplate amqpTemplate) {
        this.stringRedisTemplate = stringRedisTemplate;
        this.amqpTemplate = amqpTemplate;
    }

    @GetMapping("/redis")
    public ApiResponse<String> getRedis(@RequestParam("key") String key) {
        return ApiResponse.<String>ok().data(stringRedisTemplate.opsForValue().get(key)).build();
    }

    @PostMapping("/redis")
    public ApiResponse<String> setRedis(@RequestParam("key") String key, @RequestParam("value") String value) {
        stringRedisTemplate.opsForValue().set(key, value);
        return ApiResponse.<String>ok().build();
    }

    @PostMapping
    public ApiResponse<Long> save(@Valid @RequestBody EmployeeDTO dto) {
        return ApiResponse.<Long>ok().data(employeeService.save(dto)).build();
    }

    @GetMapping("/page")
    public ApiResponse<Page<EmployeeVO>> page(EmployeeQueryDTO dto) {
        return ApiResponse.<Page<EmployeeVO>>ok()
                .data(employeeService.page(dto))
                .build();
    }

    @GetMapping("/custom/page")
    public ApiResponse<Page<EmployeeVO>> customPage(EmployeeQueryDTO dto) {
        return ApiResponse.<Page<EmployeeVO>>ok().data(employeeService.customPage(dto)).build();
    }

    @PutMapping
    public ApiResponse<Object> update(@RequestBody EmployeeDTO dto) {
        employeeService.update(dto);
        return ApiResponse.ok().build();
    }

    @DeleteMapping
    public ApiResponse<Object> delete(@RequestParam("id") Long id) {
        Assert.notNull(id, "id不能为空");
        if (id < 0) {
            throw new RuntimeException("id不能小于0");
        }
        employeeService.deleteById(id);
        return ApiResponse.ok().build();
    }

    @GetMapping
    public ApiResponse<EmployeeVO> get(@RequestParam("id") Long id) {
        return ApiResponse.<EmployeeVO>ok().data(employeeService.getBy(id)).build();
    }

    @GetMapping("/mq")
    public ApiResponse<String> sendMessage(@RequestParam("content") String content) {
        amqpTemplate.convertAndSend(RabbitMQConfig.SUNFLOWER_EXCHANGE, RabbitMQConfig.SUNFLOWER_ROUTING_KEY, content);
        return ApiResponse.<String>ok().build();
    }
}
